﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Remote.Model;
using log4net;

namespace Remote.Service
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“RemoteArticleService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 RemoteArticleService.svc 或 RemoteArticleService.svc.cs，然后开始调试。
    public class RemoteArticleService : IRemoteArticleService
    {
        private const int FlagComplete = 101; //全本

        private readonly ILog _logger =
            LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public IList<ArticleInfo> GetGroupList(int groupid)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select c.articleid,siteid,postdate,lastupdate,c.articlename,keywords,initial,");
                strSql.Append(" authorid,author,posterid,poster,agentid,agent,sortid,typeid,intro,notice,setting,");
                strSql.Append(" lastvolumeid,lastvolume,lastchapterid,lastchapter,chapters,size,lastvisit,dayvisit,");
                strSql.Append(" weekvisit,monthvisit,allvisit,lastvote,dayvote,weekvote,monthvote,allvote,vipvotetime,");
                strSql.Append(
                    " vipvotenow,vipvotepreview,goodnum,badnum,toptime,saleprice,salenum,totalcost,articletype,");
                strSql.Append(" permission,firstflag,fullflag,imgflag,power,display,b.groupid,b.groupname,coverlink ");
                strSql.Append(" from metro_grouplist as a left join metro_group as b on a.groupid=b.groupid");
                strSql.Append(" left join jieqi_article_article as c on a.articleid=c.articleid");
                strSql.Append(" where a.groupid=?groupid");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?groupid", MySqlDbType.Int32)
                    };
                parameters[0].Value = groupid;
                IList<ArticleInfo> list = new List<ArticleInfo>();
                using (
                    MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                   System.Data.CommandType.Text, strSql.ToString(),
                                                                   parameters))
                {
                    while (dr.Read())
                    {
                        var model = new ArticleInfo();

                        #region

                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["lastupdate"] != null)
                        {
                            model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["keywords"] != null)
                        {
                            model.keywords = dr["keywords"].ToString();
                        }
                        if (dr["author"] != null)
                        {
                            model.author = dr["author"].ToString();
                        }
                        if (dr["intro"] != null)
                        {
                            model.intro = dr["intro"].ToString();
                        }
                        if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                        {
                            model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                        }
                        if (dr["lastchapter"] != null)
                        {
                            model.lastchapter = dr["lastchapter"].ToString();
                        }
                        if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                        {
                            model.chapters = int.Parse(dr["chapters"].ToString());
                        }
                        if (dr["display"] != null && dr["display"].ToString() != "")
                        {
                            model.display = int.Parse(dr["display"].ToString());
                        }
                        model.GroupID = int.Parse(dr["groupid"].ToString());
                        model.GroupName = dr["groupname"].ToString();
                        model.coverlink = dr["coverlink"].ToString();

                        #endregion

                        list.Add(model);
                    }
                    return list;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetGroupList]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ArticleInfo>();
            }
        }

        public IList<ArticleInfo> GetUpdateList(int count)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append("select * from jieqi_article_article order by lastupdate desc limit ?count");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?count", MySqlDbType.Int32)
                    };
                parameters[0].Value = count;
                IList<ArticleInfo> list = new List<ArticleInfo>();
                using (
                    MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                   System.Data.CommandType.Text, strSql.ToString(),
                                                                   parameters))
                {
                    while (dr.Read())
                    {
                        var model = new ArticleInfo();

                        #region

                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["keywords"] != null)
                        {
                            model.keywords = dr["keywords"].ToString();
                        }
                        if (dr["author"] != null)
                        {
                            model.author = dr["author"].ToString();
                        }
                        if (dr["intro"] != null)
                        {
                            model.intro = dr["intro"].ToString();
                        }
                        if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                        {
                            model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                        }
                        if (dr["lastchapter"] != null)
                        {
                            model.lastchapter = dr["lastchapter"].ToString();
                        }
                        if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                        {
                            model.chapters = int.Parse(dr["chapters"].ToString());
                        }
                        if (dr["display"] != null && dr["display"].ToString() != "")
                        {
                            model.display = int.Parse(dr["display"].ToString());
                        }

                        #endregion

                        model.GroupName = "最新更新";
                        list.Add(model);
                    }
                    return list;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetUpdateList]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ArticleInfo>();
            }
        }

        public ArticleInfo GetArticle(int articleid)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,lastupdate,articlename,keywords,author,");
                strSql.Append(" a.sortid,intro,lastchapterid,lastchapter,chapters,display,");
                strSql.Append(" b.caption as sortname,coverlink,");
                strSql.Append(" case when fullflag ='1' then '已完本' else '连载' end as writeprogress");
                strSql.Append(" from jieqi_article_article a");
                strSql.Append(" left join jieqi_article_sort b on a.sortid=b.sortid");
                strSql.Append(" where articleid=?articleid");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?articleid", MySqlDbType.Int32)
                    };
                parameters[0].Value = articleid;
                var model = new ArticleInfo();
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        #region

                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["keywords"] != null)
                        {
                            model.keywords = dr["keywords"].ToString();
                        }
                        if (dr["author"] != null)
                        {
                            model.author = dr["author"].ToString();
                        }
                        if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                        {
                            model.sortid = int.Parse(dr["sortid"].ToString());
                        }
                        if (dr["intro"] != null)
                        {
                            model.intro = dr["intro"].ToString();
                        }
                        if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                        {
                            model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                        }
                        if (dr["lastchapter"] != null)
                        {
                            model.lastchapter = dr["lastchapter"].ToString();
                        }
                        if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                        {
                            model.chapters = int.Parse(dr["chapters"].ToString());
                        }
                        if (dr["display"] != null && dr["display"].ToString() != "")
                        {
                            model.display = int.Parse(dr["display"].ToString());
                        }
                        if (dr["sortname"] != null)
                        {
                            model.sortname = dr["sortname"].ToString();
                        }
                        model.writeprogress = dr["writeprogress"].ToString();
                        model.coverlink = dr["coverlink"].ToString();

                        #endregion
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetArticle]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return null;
            }
        }

        public IList<ChapterInfo> GetMaxChapterList(int articleid)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,articlename,lastupdate,chaptername,");
                strSql.Append(" chapterorder,isvip,chaptertype,");
                strSql.Append(" a.chapterid,chapterlink,a.webflag,b.webname,previewpage,nextpage,ctype,webstate");
                strSql.Append(" from jieqi_article_chapter a ");
                strSql.Append(" join sysweb b on a.webflag=b.webflag");
                strSql.Append(
                    " join (select max(chapterid) chapterid from jieqi_article_chapter where  articleid=?articleid group by webflag) c");
                strSql.Append(" where a.articleid=?articleid and a.chapterid=c.chapterid and chaptertype=0");
                MySqlParameter[] parameters = {new MySqlParameter("?articleid", MySqlDbType.Int32)};
                parameters[0].Value = articleid;
                IList<ChapterInfo> list = new List<ChapterInfo>();
                using (
                    MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                   System.Data.CommandType.Text, strSql.ToString(),
                                                                   parameters))
                {
                    while (dr.Read())
                    {
                        var model = new ChapterInfo();

                        #region

                        if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                        {
                            model.chapterid = int.Parse(dr["chapterid"].ToString());
                        }
                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["chaptername"] != null)
                        {
                            model.chaptername = dr["chaptername"].ToString();
                        }
                        if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                        {
                            model.isvip = int.Parse(dr["isvip"].ToString());
                        }
                        if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                        {
                            model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                        }
                        if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                        {
                            model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                        }
                        if (dr["previewpage"] != null && dr["previewpage"].ToString() != "")
                        {
                            model.previewpage = int.Parse(dr["previewpage"].ToString());
                        }
                        if (dr["nextpage"] != null && dr["nextpage"].ToString() != "")
                        {
                            model.nextpage = int.Parse(dr["nextpage"].ToString());
                        }
                        if (dr["webstate"] != null && dr["webstate"].ToString() != "")
                        {
                            model.webstate = int.Parse(dr["webstate"].ToString());
                        }

                        #endregion

                        if (dr["isvip"] != null && dr["isvip"].ToString() == "1")
                        {
                            model.flag = "VIP";
                        }
                        else if (dr["ctype"] != null && dr["ctype"].ToString().Equals("1"))
                        {
                            model.flag = "图";
                        }
                        model.chapterlink = dr["chapterlink"].ToString();
                        model.webflag = dr["webflag"].ToString();
                        model.webname = dr["webname"].ToString();
                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetMaxChapterList]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ChapterInfo>();
            }
        }

        public IList<ChapterInfo> GetBookReader(int articleid, string webflag)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,articlename,lastupdate,chaptername,");
                strSql.Append(" chapterorder,isvip,chaptertype,");
                strSql.Append(" a.chapterid,chapterlink,a.webflag,b.webname,previewpage,nextpage,ctype,webstate ");
                strSql.Append(" from jieqi_article_chapter a ");
                strSql.Append(" join sysweb b on a.webflag=b.webflag");
                strSql.Append(" where chaptertype=0 and a.articleid=?articleid and a.webflag=?webflag");
                strSql.Append(" order by chapterorder");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?articleid", MySqlDbType.Int32),
                        new MySqlParameter("?webflag", MySqlDbType.String)
                    };
                parameters[0].Value = articleid;
                parameters[1].Value = webflag;
                IList<ChapterInfo> list = new List<ChapterInfo>();
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        var model = new ChapterInfo();

                        #region

                        if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                        {
                            model.chapterid = int.Parse(dr["chapterid"].ToString());
                        }
                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["chaptername"] != null)
                        {
                            model.chaptername = dr["chaptername"].ToString();
                        }
                        if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                        {
                            model.isvip = int.Parse(dr["isvip"].ToString());
                        }
                        if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                        {
                            model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                        }
                        if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                        {
                            model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                        }
                        if (dr["previewpage"] != null && dr["previewpage"].ToString() != "")
                        {
                            model.previewpage = int.Parse(dr["previewpage"].ToString());
                        }
                        if (dr["nextpage"] != null && dr["nextpage"].ToString() != "")
                        {
                            model.nextpage = int.Parse(dr["nextpage"].ToString());
                        }
                        if (dr["webstate"] != null && dr["webstate"].ToString() != "")
                        {
                            model.webstate = int.Parse(dr["webstate"].ToString());
                        }

                        #endregion

                        if (dr["isvip"] != null && dr["isvip"].ToString() == "1")
                        {
                            model.flag = "VIP";
                        }
                        else if (dr["ctype"] != null && dr["ctype"].ToString().Equals("1"))
                        {
                            model.flag = "图";
                        }
                        model.chapterlink = dr["chapterlink"].ToString();
                        model.webflag = dr["webflag"].ToString();
                        model.webname = dr["webname"].ToString();
                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetBookReader]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ChapterInfo>();
            }
        }

        public IList<ArticleInfo> GetArticleListBySort(int sortid, int begin, int count)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,lastupdate,articlename,keywords,author,");
                strSql.Append(" a.sortid,intro,lastchapterid,lastchapter,chapters,display,");
                strSql.Append(" b.caption as sortname,coverlink,");
                strSql.Append(" case when fullflag ='1' then '已完本' else '连载' end as writeprogress");
                strSql.Append(" from jieqi_article_article a");
                strSql.Append(" left join jieqi_article_sort b on a.sortid=b.sortid");
                if (sortid < FlagComplete) //分类
                {
                    strSql.Append(" where a.sortid=?sortid");
                }
                else
                {
                    if (sortid.Equals(FlagComplete))
                    {
                        strSql.Append(" where fullflag='1'");
                    }
                }
                strSql.Append(" order by lastupdate");
                strSql.Append(" limit ?pos,?count");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?sortid", MySqlDbType.Int32),
                        new MySqlParameter("?pos", MySqlDbType.Int32),
                        new MySqlParameter("?count", MySqlDbType.Int32)
                    };
                parameters[0].Value = sortid;
                parameters[1].Value = begin;
                parameters[2].Value = count;
                IList<ArticleInfo> list = new List<ArticleInfo>();

                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        #region

                        var model = new ArticleInfo();
                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["keywords"] != null)
                        {
                            model.keywords = dr["keywords"].ToString();
                        }
                        if (dr["author"] != null)
                        {
                            model.author = dr["author"].ToString();
                        }
                        if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                        {
                            model.sortid = int.Parse(dr["sortid"].ToString());
                        }
                        if (dr["intro"] != null)
                        {
                            model.intro = dr["intro"].ToString();
                        }
                        if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                        {
                            model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                        }
                        if (dr["lastchapter"] != null)
                        {
                            model.lastchapter = dr["lastchapter"].ToString();
                        }
                        if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                        {
                            model.chapters = int.Parse(dr["chapters"].ToString());
                        }
                        if (dr["display"] != null && dr["display"].ToString() != "")
                        {
                            model.display = int.Parse(dr["display"].ToString());
                        }
                        if (dr["sortname"] != null)
                        {
                            model.sortname = dr["sortname"].ToString();
                        }
                        model.writeprogress = dr["writeprogress"].ToString();
                        model.coverlink = dr["coverlink"].ToString();

                        #endregion

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetArticleListBySort]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ArticleInfo>();
            }
        }

        public int GetArticleListCountBySort(int sortid)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select count(articleid) as count from jieqi_article_article");
                if (sortid < FlagComplete) //分类
                {
                    strSql.Append(" where sortid=?sortid");
                }
                else
                {
                    if (sortid.Equals(FlagComplete))
                    {
                        strSql.Append(" where fullflag='1'");
                    }
                }
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?sortid", MySqlDbType.Int32)
                    };
                parameters[0].Value = sortid;
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        return int.Parse(dr[0].ToString());
                    }
                }
                return 0;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetArticleListCountBySort]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return 0;
            }
        }

        public IList<ChapterInfo> GetChapterListByWeb(string webflag, int articleid, int begin, int count)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,articlename,lastupdate,chaptername,");
                strSql.Append(" chapterorder,isvip,chaptertype,");
                strSql.Append(" a.chapterid,chapterlink,a.webflag,b.webname,previewpage,nextpage,ctype,webstate ");
                strSql.Append(" from jieqi_article_chapter a ");
                strSql.Append(" join sysweb b on a.webflag=b.webflag");
                strSql.Append(" where chaptertype=0 and a.articleid=?articleid and a.webflag=?webflag");
                strSql.Append(" order by chapterorder");
                strSql.Append(" limit ?pos,?count");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?articleid", MySqlDbType.Int32),
                        new MySqlParameter("?webflag", MySqlDbType.String),
                        new MySqlParameter("?pos", MySqlDbType.Int32),
                        new MySqlParameter("?count", MySqlDbType.Int32)
                    };
                parameters[0].Value = articleid;
                parameters[1].Value = webflag;
                parameters[2].Value = begin;
                parameters[3].Value = count;
                IList<ChapterInfo> list = new List<ChapterInfo>();
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        var model = new ChapterInfo();

                        #region

                        if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                        {
                            model.chapterid = int.Parse(dr["chapterid"].ToString());
                        }
                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            //int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["chaptername"] != null)
                        {
                            model.chaptername = dr["chaptername"].ToString();
                        }
                        if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                        {
                            model.isvip = int.Parse(dr["isvip"].ToString());
                        }
                        if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                        {
                            model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                        }
                        if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                        {
                            model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                        }
                        if (dr["previewpage"] != null && dr["previewpage"].ToString() != "")
                        {
                            model.previewpage = int.Parse(dr["previewpage"].ToString());
                        }
                        if (dr["nextpage"] != null && dr["nextpage"].ToString() != "")
                        {
                            model.nextpage = int.Parse(dr["nextpage"].ToString());
                        }
                        if (dr["webstate"] != null && dr["webstate"].ToString() != "")
                        {
                            model.webstate = int.Parse(dr["webstate"].ToString());
                        }

                        #endregion

                        if (dr["isvip"] != null && dr["isvip"].ToString() == "1")
                        {
                            model.flag = "VIP";
                        }
                        else if (dr["ctype"] != null && dr["ctype"].ToString().Equals("1"))
                        {
                            model.flag = "图";
                        }
                        model.chapterlink = dr["chapterlink"].ToString();
                        model.webflag = dr["webflag"].ToString();
                        model.webname = dr["webname"].ToString();
                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetChapterListByWeb]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ChapterInfo>();
            }
        }

        public int GetChapterListCountByWeb(string webflag, int articleid)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(
                    " select count(articleid) as count from jieqi_article_chapter where articleid=?articleid and webflag=?webflag");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?articleid", MySqlDbType.Int32),
                        new MySqlParameter("?webflag", MySqlDbType.String)
                    };
                parameters[0].Value = articleid;
                parameters[1].Value = webflag;
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        return int.Parse(dr[0].ToString());
                    }
                }
                return 0;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetChapterListCountByWeb]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return 0;
            }
        }

        public IList<ArticleInfo> SearchArticleList(string key, int begin, int count)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,lastupdate,articlename,keywords,author,");
                strSql.Append(" a.sortid,intro,lastchapterid,lastchapter,chapters,display,");
                strSql.Append(" b.caption as sortname,coverlink,");
                strSql.Append(" case when fullflag ='1' then '已完本' else '连载' end as writeprogress");
                strSql.Append(" from jieqi_article_article a");
                strSql.Append(" left join jieqi_article_sort b on a.sortid=b.sortid");
                strSql.Append(" where articlename like ?key");
                strSql.Append(" order by lastupdate");
                strSql.Append(" limit ?pos,?count");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?key", MySqlDbType.String),
                        new MySqlParameter("?pos", MySqlDbType.Int32),
                        new MySqlParameter("?count", MySqlDbType.Int32)
                    };
                parameters[0].Value = string.Format("%{0}%", key);
                parameters[1].Value = begin;
                parameters[2].Value = count;
                IList<ArticleInfo> list = new List<ArticleInfo>();
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        #region

                        var model = new ArticleInfo();
                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["keywords"] != null)
                        {
                            model.keywords = dr["keywords"].ToString();
                        }
                        if (dr["author"] != null)
                        {
                            model.author = dr["author"].ToString();
                        }
                        if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                        {
                            model.sortid = int.Parse(dr["sortid"].ToString());
                        }
                        if (dr["intro"] != null)
                        {
                            model.intro = dr["intro"].ToString();
                        }
                        if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                        {
                            model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                        }
                        if (dr["lastchapter"] != null)
                        {
                            model.lastchapter = dr["lastchapter"].ToString();
                        }
                        if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                        {
                            model.chapters = int.Parse(dr["chapters"].ToString());
                        }
                        if (dr["display"] != null && dr["display"].ToString() != "")
                        {
                            model.display = int.Parse(dr["display"].ToString());
                        }
                        if (dr["sortname"] != null)
                        {
                            model.sortname = dr["sortname"].ToString();
                        }
                        model.writeprogress = dr["writeprogress"].ToString();
                        model.coverlink = dr["coverlink"].ToString();

                        #endregion

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[SearchArticleList]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<ArticleInfo>();
            }
        }

        public int SearchArticleListCount(string key)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append("select count(articleid) as count from jieqi_article_article where articlename like ?key");
                MySqlParameter[] parameters =
                    {
                        new MySqlParameter("?key", MySqlDbType.String)
                    };
                parameters[0].Value = string.Format("%{0}%", key);
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        return int.Parse(dr[0].ToString());
                    }
                }
                return 0;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[SearchArticleListCount]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return 0;
            }
        }

        public IList<SysWebInfo> GetWebInfoList()
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append("select * from sysweb where isactive=1 order by sortid");
                IList<SysWebInfo> list = new List<SysWebInfo>();
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      null))
                {
                    while (dr.Read())
                    {
                        #region

                        var model = new SysWebInfo();
                        if (dr["webflag"] != null)
                        {
                            model.webflag = dr["webflag"].ToString();
                        }
                        if (dr["webname"] != null)
                        {
                            model.webname = dr["webname"].ToString();
                        }
                        if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                        {
                            model.sortid = int.Parse(dr["sortid"].ToString());
                        }
                        if (dr["matchimage"] != null)
                        {
                            model.matchimage = dr["matchimage"].ToString();
                        }
                        if (dr["matchtext"] != null)
                        {
                            model.matchtext = dr["matchtext"].ToString();
                        }
                        if (dr["filtertext"] != null)
                        {
                            model.filtertext = dr["filtertext"].ToString();
                        }
                        if (dr["encoding"] != null)
                        {
                            model.encoding = dr["encoding"].ToString();
                        }

                        #endregion

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetWebInfoList]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return new List<SysWebInfo>();
            }
        }

        public ChapterInfo GetChapter(int chapterid)
        {
            var strSql = new StringBuilder();
            try
            {
                strSql.Append(" select articleid,articlename,lastupdate,chaptername,");
                strSql.Append(" chapterorder,isvip,chaptertype,");
                strSql.Append(" a.chapterid,chapterlink,a.webflag,b.webname,previewpage,nextpage,ctype,webstate ");
                strSql.Append(" from jieqi_article_chapter a ");
                strSql.Append(" join sysweb b on a.webflag=b.webflag");
                strSql.Append(" where a.chapterid=?chapterid");
                strSql.Append(" order by chapterorder");
                MySqlParameter[] parameters = {new MySqlParameter("?chapterid", MySqlDbType.Int32)};
                parameters[0].Value = chapterid;
                var model = new ChapterInfo();
                using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                      System.Data.CommandType.Text, strSql.ToString(),
                                                                      parameters))
                {
                    while (dr.Read())
                    {
                        #region

                        if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                        {
                            model.chapterid = int.Parse(dr["chapterid"].ToString());
                        }
                        if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                        {
                            model.articleid = int.Parse(dr["articleid"].ToString());
                        }
                        if (dr["articlename"] != null)
                        {
                            model.articlename = dr["articlename"].ToString();
                        }
                        if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                        {
                            model.lastupdate =
                                model.lastupdate =
                                TimeHelper.GetDateTime(dr["lastupdate"].ToString()).ToString("yy/MM HH:mm");
                            // int.Parse(dr["lastupdate"].ToString());
                        }
                        if (dr["chaptername"] != null)
                        {
                            model.chaptername = dr["chaptername"].ToString();
                        }
                        if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                        {
                            model.isvip = int.Parse(dr["isvip"].ToString());
                        }
                        if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                        {
                            model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                        }
                        if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                        {
                            model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                        }
                        if (dr["previewpage"] != null && dr["previewpage"].ToString() != "")
                        {
                            model.previewpage = int.Parse(dr["previewpage"].ToString());
                        }
                        if (dr["nextpage"] != null && dr["nextpage"].ToString() != "")
                        {
                            model.nextpage = int.Parse(dr["nextpage"].ToString());
                        }
                        if (dr["webstate"] != null && dr["webstate"].ToString() != "")
                        {
                            model.webstate = int.Parse(dr["webstate"].ToString());
                        }

                        #endregion

                        if (dr["isvip"] != null && dr["isvip"].ToString() == "1")
                        {
                            model.flag = "VIP";
                        }
                        else if (dr["ctype"] != null && dr["ctype"].ToString().Equals("1"))
                        {
                            model.flag = "图";
                        }
                        model.chapterlink = dr["chapterlink"].ToString();
                        model.webflag = dr["webflag"].ToString();
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("[GetWebInfoList]{0}\r\n{1}", strSql.ToString(), ex.Message));
                return null;
            }
        }
    }
}
